package com.siberiadante.multiscrolldemo.view;

import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.webkit.WebView;

/**
 * @Created SiberiaDante
 * @Describe：
 * @CreateTime: 2017/12/17
 * @UpDateTime:
 * @Email: 2654828081@qq.com
 * @GitHub: https://github.com/SiberiaDante
 */

public class JudgeNestedScrollView extends NestedScrollView {
    int[] pos = new int[2];
    float yPos;
    boolean isTop;
    private boolean isNeedScroll = true;
    private float xDistance, yDistance, xLast, yLast;
    private int scaledTouchSlop;
    private boolean isWebView;
    private WebView webView;

    public JudgeNestedScrollView(Context context) {
        super(context, null);
    }
    public JudgeNestedScrollView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }
    public JudgeNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yPos = ev.getY();
                yLast = ev.getY();
                isTop = false;

                if (isWebView) {
                    if (webView != null) {
                        int scrollY = webView.getScrollY();
                        isTop = scrollY <= 0;

                        Log.d("JudgeTouch", "onInterceptTouchEvent: " + scrollY);
                        webView.getLocationInWindow(pos);
                        if (pos[1] <= 353 && isTop) {
                            webView.scrollBy(0, 1);
                        }
                    }
                }

                Log.d("JudgeTouch", "pos[1]" + pos[1]);

                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                float dy = curY - yPos;
                Log.d("onInterceptTouchEvent", "yLast" + yPos + ",curY:" + curY);
                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;
                Log.e("SiberiaDante", "xDistance ：" + xDistance + "---yDistance:" + yDistance);

                return !(xDistance >= yDistance || yDistance < scaledTouchSlop) && isNeedScroll;
        }
        Log.d("onInterceptTouchEvent", "onInterceptTouchEvent: ===========33333==========");
        return super.onInterceptTouchEvent(ev);
    }


    /*
        该方法用来处理NestedScrollView是否拦截滑动事件
         */
    public void setNeedScroll(boolean isNeedScroll) {
        this.isNeedScroll = isNeedScroll;
    }

    public void isWebView(boolean isWebView) {

        this.isWebView = isWebView;
    }

    public void setWebView(WebView webView) {

        this.webView = webView;
    }
}
